﻿using System;
using System.Collections.Generic;
using LeSoft.Util;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using System.Data;
using LeSoft.Application.Web.Controllers;

namespace LeSoft.Application.Web.Areas.AuthorizeManage.Controllers
{
    public class PermissionRoleController : MvcControllerBase
    {
        private OrganizeBLL organizeBLL = new OrganizeBLL();
        private DepartmentBLL departmentBLL = new DepartmentBLL();
        private DepartmentCache departmentCache = new DepartmentCache();
        private RoleBLL roleBLL = new RoleBLL();
        //private UserBLL userBLL = new UserBLL();
        private PermissionBLL permissionBLL = new PermissionBLL();
        private AuthorizeBLL authorizeBLL = new AuthorizeBLL();

        #region 视图功能
        /// <summary>
        /// 角色权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult AllowAuthorize()
        {
            return View();
        }
        /// <summary>
        /// 角色成员
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult AllowMember()
        {
            return View();
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 部门列表 
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="keyword">关键字</param>
        /// <returns>返回树形Json</returns>
        [HttpGet]
        public ActionResult GetDepartmentTreeJson(string OrganizeId)
        {        
            var organizedata = organizeBLL.GetList();
            if (!string.IsNullOrEmpty(OrganizeId)) organizedata = organizedata.Where(t => t.OrganizeId == OrganizeId || t.ParentId == OrganizeId).ToList();
            var data = departmentCache.GetList(OrganizeId);         
            var treeList = new List<TreeEntity>();
            #region  机构部门
            TreeEntity tree1 = new TreeEntity();
            tree1.id = "-1";
            tree1.text = "机构部门";
            tree1.value = "-1";
            tree1.isexpand = true;
            tree1.complete = true;
            tree1.hasChildren = true;
            tree1.parentId = "0";
            treeList.Add(tree1);
            foreach (OrganizeEntity obj in organizedata)
            {
                TreeEntity tree = new TreeEntity();
                tree.id = obj.OrganizeId;
                tree.text = obj.FullName;
                tree.value = obj.OrganizeId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = true;
                if (obj.ParentId == "0") tree.parentId = "-1";
                else tree.parentId = obj.ParentId;
                treeList.Add(tree);
            }
            foreach (DepartmentEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.DepartmentId) == 0 ? false : true;
                tree.id = item.DepartmentId;
                tree.text = item.FullName;
                tree.value = item.DepartmentId;
                if (item.ParentId == "0"||item.ParentId.IsEmpty())
                {
                    tree.parentId =item.OrganizeId;
                }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            #endregion

            TreeEntity tree2= new TreeEntity();
            tree2.id = "-2";
            tree2.text = "其他用户";
            tree2.value = "-2";
            tree2.isexpand = true;
            tree2.complete = true;
            tree2.hasChildren = true;
            tree2.parentId = "0";
            treeList.Add(tree2);
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult GetUserListJson(string roleId,string DepartmentId)
        {
            var existMember = permissionBLL.GetMemberList(roleId);
            if (DepartmentId == "card-box") DepartmentId = "";
            var userdata = new WorkerBLL().GetTable(DepartmentId, "Department");
            userdata.Columns.Add("ischeck", Type.GetType("System.Int32"));
            userdata.Columns.Add("isdefault", Type.GetType("System.Int32"));
            foreach (DataRow item in userdata.Rows)
            {
                string UserId = item["id"].ToString();
                int ischeck = existMember.Count(t => t.UserId == UserId);
                item["ischeck"] = ischeck;
                if (ischeck > 0)
                {
                    item["isdefault"] = existMember.First(t => t.UserId == UserId).IsDefault;
                }
                else
                {
                    item["isdefault"] = 0;
                }
            }
            userdata = DataHelper.DataFilter(userdata, "", "ischeck desc");
            return Content(userdata.ToJson());
        }
        /// <summary>
        /// 系统功能列表
        /// </summary>
        /// <param name="RoleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ModuleTreeJson(string roleId)
        {
            var existModule = permissionBLL.GetModuleList(roleId);
          //  var data = StaticClass.AuthorizeManager.ModuleList;
           
           var data = authorizeBLL.GetModuleList(SystemInfo.CurrentUserId);
            var treeList = new List<TreeEntity>();
            foreach (ModuleEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.ModuleId) == 0 ? false : true;
                tree.id = item.ModuleId;
                tree.text = item.FullName;
                tree.value = item.ModuleId;
                tree.title = "";
                tree.checkstate = existModule.Count(t => t.ItemId == item.ModuleId);
                tree.showcheck = true;
                tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = item.ParentId;
                tree.img = item.Icon;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 系统按钮列表
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ModuleButtonTreeJson(string roleId)
        {
            var existModuleButton = permissionBLL.GetModuleButtonList(roleId);
            //var moduleData = StaticClass.AuthorizeManager.ModuleList;
            //var moduleButtonData = StaticClass.AuthorizeManager.ModuleButtonList;
            var moduleData = authorizeBLL.GetModuleList(SystemInfo.CurrentUserId);
            var moduleButtonData = authorizeBLL.GetModuleButtonList(SystemInfo.CurrentUserId);
            var treeList = new List<TreeEntity>();
            foreach (ModuleEntity item in moduleData)
            {
                if (item.IsMenu == 1)
                {
                    bool hasChildren = moduleButtonData.Count(t => t.ModuleId == item.ModuleId) == 0 ? false : true;
                    if (hasChildren == false)
                    {
                        continue;
                    }
                }
                TreeEntity tree = new TreeEntity();
                tree.id = item.ModuleId;
                tree.text = item.FullName;
                tree.value = item.ModuleId;
                tree.checkstate = existModuleButton.Count(t => t.ItemId == item.ModuleId);
                tree.showcheck = true;
                tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = true;
                tree.parentId = item.ParentId;
                tree.img = item.Icon;
                treeList.Add(tree);
            }
            foreach (ModuleButtonEntity item in moduleButtonData)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = moduleButtonData.Count(t => t.ParentId == item.ModuleButtonId) == 0 ? false : true;
                tree.id = item.ModuleButtonId;
                tree.text = item.FullName;
                tree.value = item.ModuleButtonId;
                if (item.ParentId == "0")
                {
                    tree.parentId = item.ModuleId;
                }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.checkstate = existModuleButton.Count(t => t.ItemId == item.ModuleButtonId);
                tree.showcheck = true;
                tree.isexpand = false;
                tree.complete = true;
                tree.img = "fa fa-wrench " + item.ModuleId;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 系统视图列表
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ModuleColumnTreeJson(string roleId)
        {
            var existModuleColumn = permissionBLL.GetModuleColumnList(roleId);
            var moduleData = StaticClass.AuthorizeManager.ModuleList;          
           // var moduleData = authorizeBLL.GetModuleList(SystemInfo.CurrentUserId);
            var moduleColumnData = authorizeBLL.GetModuleColumnList(SystemInfo.CurrentUserId);
            var treeList = new List<TreeEntity>();
            foreach (ModuleEntity item in moduleData)
            {
                TreeEntity tree = new TreeEntity();
                tree.id = item.ModuleId;
                tree.text = item.FullName;
                tree.value = item.ModuleId;
                tree.checkstate = existModuleColumn.Count(t => t.ItemId == item.ModuleId);
                tree.showcheck = true;
                tree.isexpand = false;
                tree.complete = true;
                tree.hasChildren = true;
                tree.parentId = item.ParentId;
                tree.img = item.Icon;
                treeList.Add(tree);
            }
            foreach (ModuleColumnEntity item in moduleColumnData)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = moduleColumnData.Count(t => t.ParentId == item.ModuleColumnId) == 0 ? false : true;
                tree.id = item.ModuleColumnId;
                tree.text = item.FullName;
                tree.value = item.ModuleColumnId;
                if (item.ParentId == "0")
                {
                    tree.parentId = item.ModuleId;
                }
                else
                {
                    tree.parentId = item.ParentId;
                }
                tree.checkstate = existModuleColumn.Count(t => t.ItemId == item.ModuleColumnId);
                tree.showcheck = true;
                tree.isexpand = false;
                tree.complete = true;
                tree.img = "fa fa-filter " + item.ModuleId;
                tree.hasChildren = hasChildren;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }
        /// <summary>
        /// 数据权限列表
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult OrganizeTreeJson(string roleId, int AuthorizeDataType)
        {
            var existAuthorizeData = permissionBLL.GetAuthorizeDataList(roleId);
            var treeList = new List<TreeEntity>();
            if (AuthorizeDataType == -1) return null;
            else
            {
                if (AuthorizeDataType > -1)
                {
                    #region 机构
                    var organizedata = organizeBLL.GetList();
                    foreach (OrganizeEntity item in organizedata)
                    {
                        TreeEntity tree = new TreeEntity();
                        bool hasChildren = organizedata.Count(t => t.ParentId == item.OrganizeId) == 0 ? false : true;
                        if (AuthorizeDataType > 0) hasChildren = true;
                        tree.id = item.OrganizeId;
                        tree.text = item.FullName;
                        tree.value = item.OrganizeId;
                        tree.title = "0";//机构
                        tree.parentId = item.ParentId;
                        if (item.ParentId == "0")
                        {
                            tree.img = "fa fa-sitemap";
                        }
                        else
                        {
                            tree.img = "fa fa-home";
                        }
                        tree.checkstate = existAuthorizeData.Count(t => t.ResourceId == item.OrganizeId);
                        tree.showcheck = true;
                        tree.isexpand = true;
                        tree.complete = true;
                        tree.hasChildren = hasChildren;
                        treeList.Add(tree);
                    }
                    #endregion
                }
                if (AuthorizeDataType > 0)
                {
                    var pstructdata = new PStructBLL().GetList().Where(t => t.Type <= AuthorizeDataType && t.Type != 3).OrderBy(t => t.Code);
                    foreach (PStructEntity item in pstructdata)
                    {
                        TreeEntity tree = new TreeEntity();
                        bool hasChildren = pstructdata.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                        tree.id = item.Id;
                        tree.text = item.Name;
                        tree.value = item.Id;
                        tree.title = item.Type.ToString();//房产类型
                        if (item.ParentId == "0")
                        {
                            tree.parentId = item.OrganizeId;
                        }
                        else
                        {
                            tree.parentId = item.ParentId;
                        }
                        tree.checkstate = existAuthorizeData.Count(t => t.ResourceId == item.Id);
                        tree.showcheck = true;
                        tree.isexpand = true;
                        tree.complete = true;
                        tree.img = "fa fa-umbrella";
                        tree.hasChildren = hasChildren;
                        treeList.Add(tree);
                    }
                }
            }
            int authorizeType = -1;
            if (existAuthorizeData.ToList().Count > 0)
            {
                authorizeType = existAuthorizeData.ToList()[0].AuthorizeType.ToInt();
            }
            var JsonData = new
            {
                authorizeType = authorizeType,
                authorizeData = existAuthorizeData,
                treeJson = treeList.TreeToJson(),
            };
            return Content(JsonData.ToJson());
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存角色成员
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="userIds">成员Id</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveMember(string roleId, string userIds)
        {
            permissionBLL.SaveMember(AuthorizeTypeEnum.Role, roleId, userIds);
            return Success("保存成功。");
        }
        /// <summary>
        /// 保存角色授权
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="moduleIds">功能Id</param>
        /// <param name="moduleButtonIds">按钮Id</param>
        /// <param name="moduleColumnIds">视图Id</param>
        /// <param name="authorizeDataJson">数据权限</param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveAuthorize(string roleId, string moduleIds, string moduleButtonIds, string moduleColumnIds, int AuthorizeDataType, string authorizeDataJson)
        {
            permissionBLL.SaveAuthorize(AuthorizeTypeEnum.Role, roleId, moduleIds, moduleButtonIds, AuthorizeDataType,   authorizeDataJson);
            return Success("保存成功。");
        }
        #endregion
    }
}
